
    /**  
    * @Title: ScheduleInfoController.java
    * @Package com.efast.cafe.commponent.job.controller
    * @Description: TODO(用一句话描述该文件做什么)
    * @author Administrator
    * @date 2018年9月13日
    * @version V1.0  
    */
    
package com.efast.cafe.commponent.job.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.cafe.commponent.job.bean.ScheduleExecuteLog;
import com.efast.cafe.commponent.job.bean.ScheduleJobAddBean;
import com.efast.cafe.commponent.job.bean.ScheduleJobBean;
import com.efast.cafe.commponent.job.bean.ScheduleJobParamBean;
import com.efast.cafe.commponent.job.bean.ScheduleJobQueryBean;
import com.efast.cafe.commponent.job.service.CafeScheduleService;
import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.exception.ServiceException;

/**
    * @ClassName: ScheduleInfoController
    * @Description: TODO(这里用一句话描述这个类的作用)
    * @author Administrator
    * @date 2018年9月13日
    *
    */
@Controller
@RequestMapping(value="/schedule")
public class ScheduleInfoController {

	@Autowired
	private CafeScheduleService cafeScheduleService;
	
	@RequestMapping(value="/info")
	public @ResponseBody ResultBean queryScheduleInfo(){
		ResultBean res = new ResultBean(true, "");
		Map<String, Object> map =  cafeScheduleService.getSchedulerInfo();
		res.setObject(map);
		return res;
	}
	
	@RequestMapping(value="/addJob")
	public @ResponseBody ResultBean addJob(@RequestBody ScheduleJobAddBean bean){
		ResultBean res = new ResultBean(true, "");
		try{
			List<ScheduleJobParamBean> list = bean.getParams();
			Map<String, Object> params = new HashMap<>();
			if(null!=list && !list.isEmpty()){
				for(ScheduleJobParamBean p : list){
					params.put(p.getKey(), p.getValue());
				}
			}
			cafeScheduleService.addjob(bean.getJobName(), bean.getJobClass(), 
					bean.getCron(), bean.getJobDesc(), params,bean.isStartNow());
		}catch(Exception e){
			res.setSuccess(false);
			if(e instanceof ServiceException){
				res.setErrorCode(e.getMessage());
			}else{
				res.setErrorCode("SYSTEM_ERROR");
			}
			res.setMessage(e.getMessage());
		}
		return res;
	}
	
	
	@RequestMapping(value="/updateJob")
	public @ResponseBody ResultBean updateJob(@RequestBody ScheduleJobAddBean bean){
		ResultBean res = new ResultBean(true, "");
		try{
			List<ScheduleJobParamBean> list = bean.getParams();
			Map<String, Object> params = new HashMap<>();
			if(null!=list && !list.isEmpty()){
				for(ScheduleJobParamBean p : list){
					params.put(p.getKey(), p.getValue());
				}
			}
			cafeScheduleService.modifyJob(bean.getJobName(),bean.getJobClass(), 
					 bean.getCron(), bean.getJobDesc(), params,bean.isStartNow());
		}catch(Exception e){
			res.setSuccess(false);
			if(e instanceof ServiceException){
				res.setErrorCode(e.getMessage());
			}else{
				res.setErrorCode("SYSTEM_ERROR");
			}
			res.setMessage(e.getMessage());
		}
		return res;
	}
	
	
	@RequestMapping(value="/resumeJob")
	public @ResponseBody ResultBean resumeJob(@RequestBody ScheduleJobAddBean bean){
		ResultBean res = new ResultBean(true, "");
		try{
			cafeScheduleService.resumejob(bean.getJobName());
		}catch(Exception e){
			res.setSuccess(false);
			if(e instanceof ServiceException){
				res.setErrorCode(e.getMessage());
			}else{
				res.setErrorCode("SYSTEM_ERROR");
			}
			res.setMessage(e.getMessage());
		}
		return res;
	}
	
	
	/**
	 * 全部恢复启动job
	 * @return
	 */
	@RequestMapping(value="/resumeAllJobs")
	public @ResponseBody ResultBean resumeAllJobs(){
		ResultBean res = new ResultBean(true, "");
		try{
			cafeScheduleService.resumeAllJobs();
		}catch(Exception e){
			res.setSuccess(false);
			if(e instanceof ServiceException){
				res.setErrorCode(e.getMessage());
			}else{
				res.setErrorCode("SYSTEM_ERROR");
			}
			res.setMessage(e.getMessage());
		}
		return res;
	}
	
	
	/**
	 * 暂停job
	 * @return
	 */
	@RequestMapping(value="/pauseJob")
	public @ResponseBody ResultBean pauseJob(@RequestBody ScheduleJobAddBean bean){
		ResultBean res = new ResultBean(true, "");
		try{
			cafeScheduleService.pausejob(bean.getJobName());
		}catch(Exception e){
			res.setSuccess(false);
			if(e instanceof ServiceException){
				res.setErrorCode(e.getMessage());
			}else{
				res.setErrorCode("SYSTEM_ERROR");
			}
			res.setMessage(e.getMessage());
		}
		return res;
	}
	
	
	/**
	 * 全部暂停job
	 * @return
	 */
	@RequestMapping(value="/pauseAllJobs")
	public @ResponseBody ResultBean pauseAllJobs(){
		ResultBean res = new ResultBean(true, "");
		try{
			cafeScheduleService.pauseAllJobs();
		}catch(Exception e){
			res.setSuccess(false);
			if(e instanceof ServiceException){
				res.setErrorCode(e.getMessage());
			}else{
				res.setErrorCode("SYSTEM_ERROR");
			}
			res.setMessage(e.getMessage());
		}
		return res;
	}
	
	
	/**
	 * 删除job
	 * @return
	 */
	@RequestMapping(value="/removeJob")
	public @ResponseBody ResultBean removeJob(@RequestBody ScheduleJobAddBean bean){
		ResultBean res = new ResultBean(true, "");
		try{
			cafeScheduleService.deletejob(bean.getJobName());
		}catch(Exception e){
			res.setSuccess(false);
			if(e instanceof ServiceException){
				res.setErrorCode(e.getMessage());
			}else{
				res.setErrorCode("SYSTEM_ERROR");
			}
			res.setMessage(e.getMessage());
		}
		return res;
	}
	
	
	@RequestMapping(value="/jobList")
	public @ResponseBody ResultBean queryAllJobs(){
		ResultBean res = new ResultBean(true, "");
		try{
			List<ScheduleJobBean> list = cafeScheduleService.getAllJobs();
			res.setObject(list);
		}catch(Exception e){
			res.setSuccess(false);
			if(e instanceof ServiceException){
				res.setErrorCode(e.getMessage());
			}else{
				res.setErrorCode("SYSTEM_ERROR");
			}
			res.setMessage(e.getMessage());
		}
		return res;
	}
	
	@RequestMapping(value="/jobPageList")
	public @ResponseBody ResultBean queryPageJobs(@RequestBody ScheduleJobQueryBean queryBean){
		return new ResultBean(true, cafeScheduleService.queryJobPageList(queryBean));
	}
	
	@RequestMapping(value="/jobDetail")
	public @ResponseBody ResultBean queryJobDetail(@RequestBody ScheduleJobQueryBean queryBean){
		try {
			return new ResultBean(true, "",cafeScheduleService.queryDetail(queryBean.getJobName()));
		} catch (Exception e) {
			return new ResultBean(false);
		}
	}
	
	@RequestMapping(value="/jobLogs")
	public @ResponseBody ResultBean queryJobLogs(@RequestBody ScheduleJobQueryBean queryBean){
		ScheduleExecuteLog log = ScheduleExecuteLog.getInstance(queryBean.getJobName());
		return new ResultBean(true, "", log.getLogs());
	}
}
